//
//  GRLogisticsInforViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/24.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRLogisticsInforViewController.h"
#import "GRReturnTimeCell.h"
#import "RequestManager+GROrderManager.h"
#import "GRReturnResonModel.h"
@interface GRLogisticsInforViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *logisticsInfoList;

@property (strong, nonatomic) IBOutlet UIView *headerView;
@property(nonatomic,strong)NSMutableArray * dataSourceArr;
@property (weak, nonatomic) IBOutlet UILabel *NameLabel;
@property (weak, nonatomic) IBOutlet UILabel *logsticSnLabel;

@property (weak, nonatomic) IBOutlet UIButton *replicationBt;

@end

@implementation GRLogisticsInforViewController
-(NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.replicationBt.layer setBorderWidth:1];
    [self.replicationBt.layer setBorderColor:HEXCOLOR(0xDDDDDD).CGColor];
    [self.logisticsInfoList registerNib:[UINib nibWithNibName:@"GRReturnTimeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"timeCell"];
    self.logisticsInfoList.tableHeaderView = self.headerView;
    self.navigationItem.title = @"物流信息";
    [self loadData];
    
}
-(void)loadData{
    [RequestManager getExpressQuery:self.orderId CompleteHander:^(BOOL success, id responseObject, GRError *error) {
        NSLog(@"%@",responseObject);
        if (success) {
            self.NameLabel.text = responseObject[@"logisticsType"];
            self.logsticSnLabel.text = responseObject[@"logisticsNo"];
            NSArray * listArr = responseObject[@"list"];
            if (Array(listArr)) {
                for (NSDictionary * dic  in listArr) {
                    GRLogsticProgresModel * logsticModel = [GRLogsticProgresModel yy_modelWithDictionary:dic];
                    [self.dataSourceArr addObject:logsticModel];
                }
            }
            [self.logisticsInfoList reloadData];
        }else{
            [GRHUD showMessage:error.errorMsg];
        }
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GRReturnTimeCell * timeCell = [tableView dequeueReusableCellWithIdentifier:@"timeCell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        timeCell.topLine.hidden = YES;
        timeCell.colorView.hidden = NO;
        timeCell.textLabel.textColor = HEXCOLOR(0x222222);
    }else{
        timeCell.topLine.hidden = NO;
        timeCell.colorView.hidden = YES;
        timeCell.textLabel.textColor = HEXCOLOR(0x999999);
    }
    if (indexPath.row == _dataSourceArr.count -1 ) {
        timeCell.bottomLine.hidden = YES;
    }else{
         timeCell.bottomLine.hidden = NO;
    }
    GRLogsticProgresModel * model = self.dataSourceArr[indexPath.row];
    timeCell.titleLabel.text = model.status;
    timeCell.leftTimLabel.text = model.time;
    
    return timeCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height =  45;
    GRLogsticProgresModel * model = self.dataSourceArr[indexPath.row];
    height += [model.status boundingRectWithSize:CGSizeMake(KscreenWidth-60, MAXFLOAT) options:(NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height;
    return height;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}


- (IBAction)copyAction:(id)sender {
    UIPasteboard * pastBoard = [UIPasteboard generalPasteboard];
    pastBoard.string = self.logsticSnLabel.text;
    [GRHUD showMessage:@"复制成功"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
